@extends('admin.layouts.index')
@section('title', '黑名单管理')

@section('content')
<link rel="stylesheet" href="/assets/css/links_detial.css?v=2022062009">

<meta name="csrf-token" content="{{ csrf_token() }}">
<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-body">
                <form class="m-search" method="GET" action="{{ URL::current() }}">
                    <div class="m-search-items">
                        <div class="m-search-item">
                            <label>名称黑名单</label>
                            <input type="text" name="name" placeholder="请输入名称黑名单">
                        </div>
                        <div class="m-search-item">
                            <label>黑名单链接</label>
                            <input type="text" name="domain" placeholder="请输入黑名单链接">
                        </div>
                        <div class="m-search-item">
                            <button class="button">立即搜索</button>
                        </div>
                    </div>
                    <div class="m-search-handle">
                        <a class="button" href="javascript:;" onclick="showmask(0,0,'','')">
                            <i class="bx bx-plus-circle"></i>
                            <font>添加黑名单</font>
                        </a>
                    </div>
                </form>
                <div style="overflow-x: scroll;">
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th scope="col" class="table-td-phone">ID</th>
                                <th scope="col">黑名单名称</th>
                                <th scope="col">黑名单链接</th>
                                <th scope="col">添加时间</th>
                                <th scope="col">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($blacklists as $blacklist)
                            <tr>
                                <td class="table-td-phone">{{ $blacklist->id }}</td>
                                <td>{{ $blacklist->name }}</td>
                                <td onclick="successNotice('{{ $blacklist->domain }}','黑名单链接')">{{ $blacklist->domain }}</td>
                                <td>{{ $blacklist->created_at }}</td>
                                <td><a style="margin: 0 10px 0 0;cursor:pointer;" onclick="showmask('{{ $blacklist->id }}',1,'{{ $blacklist->name }}','{{ $blacklist->domain }}')">更新</a>|<a style="cursor:pointer;margin: 0 0 0 10px" onclick="showmask('{{ $blacklist->id }}',0,'','')">删除</a></td>                              
                            </tr>
                            @empty
                            <tr>
                                <td colspan="9">您未添加黑名单,您可以<a style="margin: 0px 3px;" href="#" onclick="showmask(0,0,'','')">添加二维码的黑名单链接</a>！</td>
                            </tr>
                            @endforelse
                        </tbody>
                    </table>

                    <div class="pagination-container">
                        {!! $blacklists->appends($condition)->render() !!}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="addblack" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content modal-self-cotain">
            <div class="modal-self-body">
                <div id="qrcodesImagesMore" style="width:100%;float: left;margin-top: 40px;">
                    <div class="row" id="updateblacklist">
                        <div class="m-form-item">
                            <label><b>*</b>黑名单备注内容:<font>用于区分不同的黑名单</font></label>
                            <input type="text" class="m-input" name="" placeholder="请输入20字以内备注内容。" id="name">
                        </div>
                        <div class="m-form-item">
                            <label><b>*</b>黑名单链接:<font>请输入识别后的二维码链接</font></label>
                            <input type="text" class="m-input" name="" placeholder="最长1024字符。" id="domain">
                        </div>
                        <input type="hidden" id="black_id">
                    </div>
                    <div class="row" id="deleteblack">
                        <div class="col-lg-12 mb-3">
                            <label>是否删除此黑名单</label>
                            <div>
                                如不再检测此链接可选择删除。
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-self-footer">
                <button type="button" class="btn btn-primary waves-light" onclick="blackSubmit();" id="buttonUpdate">立即更新</button>
                <button type="button" class="btn btn-border" data-bs-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<script src="/js/clipboard.min.js"></script>
<script src="/assets/moment/moment.js"></script>
<script src="/assets/moment/locale/zh-cn.js"></script>
<script type="text/javascript">
    function showmask(id,type,name,domain){
        $("#black_id").val(id);
        if(id == 0 || type==1){
            $('#updateblacklist').show();
            $('#deleteblack').hide();
            var text = id > 0 ? '立即更新':'立即添加';
            $('#buttonUpdate').text(text);
            if(id > 0){
                $('#name').val(name);
                $('#domain').val(domain);
            }else{
                $('#name').val('');
                $('#domain').val('');
            }
        }else{
            $('#updateblacklist').hide();
            $('#deleteblack').show();
            $('#buttonUpdate').text('立即删除');
        }
        $("#addblack").modal('toggle');
    }

    function blackSubmit() {
        var name = $('#name').val();
        var domain = $('#domain').val();
        if($('#buttonUpdate').text() == '立即删除'){
            window.location.href = '/admin/black/delete_black?id='+$("#black_id").val();
        }else{
            if(name.length==0 || domain.length==0){
                mviewToastr("error","备注或链接不能为空");
                return;
            }else{
                $.ajax({
                    url:"/admin/black/upblacklist",
                    data: {
                        "name":name,
                        "domain":domain,
                        "id":$("#black_id").val(),
                    },
                    type: 'GET',
                    success:function(result){
                        if (result.status == "success") {
                            mviewToastr("success",result.msg);
                            window.location.reload();
                        } else {
                            mviewToastr("error",result.msg);
                        }
                    },
                    error:function(error){
                        mviewToastr("error","请求失败,请联系客服处理!");
                    }
                });
            } 
        }
        
    }
    
    
</script>
@endsection